package com.company.CollectionDemo;

import java.util.*;
import java.util.stream.Collectors;

import com.company.LambdaDemo.Student;
import com.company.Thread.UserInfo;

import cn.hutool.core.util.RandomUtil;

public class HashMapDemo {
    public static void main(String[] args) {
        HashMap<Integer, String> test = new HashMap<>();
        test.put(1, "123");
        test.putIfAbsent(1, "1234214");

        // testGroupBy();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 1000000; i++) {
            sb.append(RandomUtil.BASE_CHAR_NUMBER);
        }
        String strTest = sb.toString();
        System.out.println(strTest.length());
        HashMap<Integer, UserInfo> hashMap = new HashMap<>();
        UserInfo userInfo = new UserInfo();
        userInfo.setSex(10);
        hashMap.put(1, userInfo);

        hashMap.get(1).setSex(14);
        Integer[] array = hashMap.keySet().toArray(new Integer[0]);
        System.out.println(hashMap.get(1).getSex());
    }

    static void testGroupBy() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(100 - i);
        }

        System.out.println(list.stream().sorted().collect(Collectors.toList()));

        Map<Integer, List<Integer>> map = list.stream().collect(Collectors.groupingBy(key -> {
            return key % 8;
        }));
        System.out.println(map);

        Map<Integer, List<Integer>> sortMap = list.stream().collect(Collectors.groupingBy(key -> {
            return key % 8;
        }, LinkedHashMap::new, Collectors.toList()));

        System.out.println(sortMap);
    }

    static void test() {
        HashMap<Object, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            Object obj = new Object();
            System.out.println(obj.hashCode());
            if (hashMap.containsKey(obj)) {
                System.out.println(obj);
            }

            hashMap.put(obj, i);
        }

        List<Student> students = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Student student = new Student(i % 3, String.valueOf(i));
            students.add(student);
        }

        Map<Integer, Integer> map =
            students.stream().collect(Collectors.toMap(Student::getId, Student::getId, (v1, v2) -> v2));
        System.out.println(map);
        Integer value = map.get(100);
        System.out.println(value);
        Map.Entry<Integer, Long> entry = new Map.Entry<Integer, Long>() {
            @Override
            public Integer getKey() {
                return 1;
            }

            @Override
            public Long getValue() {
                return 2L;
            }

            @Override
            public Long setValue(Long value) {
                return value;
            }
        };
        entry.setValue(2L);
        System.out.println(entry.getValue());
    }
}
